package com.axin.blog.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.axin.blog.api.domain.Blog;
import com.axin.blog.api.dto.BlogDTO;
import com.axin.blog.api.vo.BlogVo;
import com.axin.blog.service.IBlogCategoryService;
import com.axin.blog.service.IBlogService;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.security.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 博客 接口控制
 *
 * @Author axin
 * @Date 2021/10/29 21:02
 **/
@RestController
@RequestMapping("/blog")
public class BlogController extends BaseController {

    @Autowired
    private IBlogService blogService;

    /**
     * 根据参数分页查询博客分类列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:list')")
    @GetMapping("/list")
    public TableDataInfo list(Blog blog) {
        return blogService.selectPageBlogList(blog);
    }


    /**
     * 新增博客
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:add')")
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody BlogDTO blogDTO) {
        blogDTO.setUserId(getUserId());
        blogDTO.setAuthor(getNickName());
        return toAjax(blogService.insertBlog(blogDTO));
    }


    /**
     * 根据博客ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:query')")
    @GetMapping(value = "/{blogId}")
    public AjaxResult<BlogVo> getInfo(@PathVariable Long blogId) {
        return AjaxResult.success(blogService.selectBlogById(blogId));
    }

    /**
     * 修改博客
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BlogDTO blogDTO) {
        return toAjax(blogService.updateBlog(blogDTO));
    }


    /**
     * 删除博客
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:remove')")
    @DeleteMapping("/{blogIds}")
    public AjaxResult remove(@PathVariable Long[] blogIds) {
        blogService.deleteBlogByIds(blogIds);
        return success();
    }

    /***********************************前台接口**********************************************************************/
    /**
     * 获取博客列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getNewBlogPageList")
    TableDataInfo getNewBlogPageList(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize,
                                     @RequestParam(name = "createMonth", required = false, defaultValue = "") String createMonth,
                                     @RequestParam(name = "categoryId", required = false, defaultValue = "") Long categoryId,
                                     @RequestParam(name = "tagId", required = false, defaultValue = "") Long tagId) {
        Blog blog = new Blog();
        if(StrUtil.isNotEmpty(createMonth)) {
            blog.setCreateMonth(createMonth);
        }
        if (ObjectUtil.isNotNull(categoryId)) {
            blog.setCategoryId(categoryId);
        }
        if (ObjectUtil.isNotNull(tagId)) {
            blog.setTagIds(String.valueOf(tagId));
        }

        return blogService.getNewBlogPageList(blog);
    }

    /**
     * 根据ID获取详情
     *
     * @param blogId
     * @return
     */
    @GetMapping("/getBlogDetail/{blogId}")
    public AjaxResult<BlogVo> getBlogDetail(@PathVariable("blogId") Long blogId) {
        return AjaxResult.success(blogService.selectBlogById(blogId));
    }


    /**
     * 获取时间归档
     *
     * @return
     */
    @GetMapping("/getMonthArchiveList")
    public AjaxResult getMonthArchiveList() {
        return AjaxResult.success(blogService.getMonthArchiveList());
    }


}
